#include <linux/types.h>
#include <linux/ipc.h>
#include <linux/shm.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <linux/stat.h>
#include <fcntl.h>

#define SHMSZ 27
char SEM_NAME[] = "vik";

int main(void)
{
    char ch;
    int shmid;
    key_t key;
    char *shm, *s;
    sem_t *mutex;

    //name the shared memory segment
    key = 1000;
    //create & initialize existing semaphore
    mutex = sem_open(SEM_NAME, 0, 0644, 0);

    if (mutex == SEM_FAILED)
    {
        perror("reader: unable to execute semaphore");
        sem_close(mutex);
        exit(-1);
    }

    //create the shared memory segment with this key
    shmid = shmget(key, SHMSZ, 0666);
    if (shmid < 0)
    {
        perror("reader: failure in shmget");
        exit(-1);
    }

    //attach this segment to virtual memory
    shm = (char*)shmat(shmid, NULL, 0);
    //start reading
    s = shm;
    for (s = shm; *s != NULL; s++)
    {
        sem_wait(mutex);
        putchar(*s);
        sem_post(mutex);
    }

    printf("\n");

    //once done signal exiting of reader. This can be replace by another semaphore
    *shm = '*';
    sem_close(mutex);
    shmctl(shmid, IPC_RMID, 0);
    exit(0);
}
